define([
    'base',
    'bootstrapDialog',
    'msgTool',
    'bootstrapTable',
    'jqViewer',
    'jqPrint',
    "css!global/plugins/viewer/viewer.min.css"
], function (base, BootstrapDialog,msgTool,dateTool) {
    //默认加载基础库
    base();
    return {
        list: function () {
            var API_URL = "api.json";
            var myTable = $('#dataList');
            var tableRefresh = function () {
                myTable.bootstrapTable('refresh', {url: API_URL})
            };
            //table按需刷新动作
            var doAction = function (obj) {
                myTable.bootstrapTable('refresh', {url: API_URL + '?pid=' + obj.id})
            };

            //table当前行的响应事件
            window.operateEvents = {
                'click .issue': function (e, value, row) {
                    htmlModal("执照签发", row);
                },
                // 'click .sendSMS': function (e, value, row) {
                //     BootstrapDialog.show({
                //         title: "短信通知",
                //         message: "确定要发送短信给《" + row.itemName + "》的法人和代办人吗？",
                //         draggable: true,
                //         type: BootstrapDialog.TYPE_DEFAULT,
                //         buttons: [{
                //             label: '取消',
                //             cssClass: 'btn-white',
                //             icon: 'fa fa-times',
                //             action: function (dialogItself) {
                //                 dialogItself.close();
                //             }
                //         }, {
                //             label: '发送',
                //             cssClass: 'btn-primary',
                //             icon: 'fa fa-check',
                //             action: function (dialogItself) {
                //                 $.ajax({
                //                     url: "/sendSMS/action",
                //                     method: "POST",
                //                     dataType: "json",
                //                     data: {},
                //                     success: function (data) {
                //                         if (data.success) {
                //                             msgTool.success("恭喜您，短信通知成功！", "信息提示");
                //                             tableRefresh();
                //                             dialogItself.close();
                //                             return false;
                //                         } else {
                //                             msgTool.error("警告，" + data.msg, "错误提示");
                //                             tableRefresh();
                //                             dialogItself.close();
                //                             return false;
                //                         }
                //                     },
                //                     error: function (jqXHR) {
                //                         msgTool.error(jqXHR.status + " (" + jqXHR.statusText + ")", "错误提示");
                //                         tableRefresh();
                //                         dialogItself.close();
                //                         return false;
                //                     }
                //                 })
                //             }
                //         }]
                //     });
                // },
                'click .resetReceive': function (e, value, row) {
                    BootstrapDialog.show({
                        title: "领取重置",
                        message: "确定要重置《" + row.itemName + "》营业执照领取状态吗？",
                        draggable: true,
                        type: BootstrapDialog.TYPE_DEFAULT,
                        buttons: [{
                            label: '取消',
                            cssClass: 'btn-white',
                            icon: 'fa fa-times',
                            action: function (dialogItself) {
                                dialogItself.close();
                            }
                        }, {
                            label: '重置未领取',
                            cssClass: 'btn-warning',
                            icon: 'fa fa-clock-o',
                            action: function (dialogItself) {
                                $.ajax({
                                    url: "/merchantIssue/resetReceive/wait",
                                    method: "POST",
                                    data: {
                                        itemId: row.itemId,
                                        itemType: row.itemType
                                    },
                                    success: function (data) {
                                        console.log(data);
                                        if (data.success) {
                                            msgTool.success("恭喜您，领取状态已重置为未领取！", "信息提示");
                                            tableRefresh();
                                            dialogItself.close();
                                            return false;
                                        } else {
                                            msgTool.error("警告，" + data.msg, "错误提示");
                                            tableRefresh();
                                            dialogItself.close();
                                            return false;
                                        }
                                    },
                                    error: function (jqXHR) {
                                        msgTool.error(jqXHR.status + " (" + jqXHR.statusText + ")", "错误提示");
                                        tableRefresh();
                                        dialogItself.close();
                                        doAction();
                                        return false;
                                    }
                                })
                            }
                        },
                            {
                                label: '重置已领取',
                                cssClass: 'btn-primary',
                                icon: 'fa fa-check',
                                action: function (dialogItself) {
                                    $.ajax({
                                        url: "/merchantIssue/resetReceive/success",
                                        method: "POST",
                                        data: {
                                            itemId: row.itemId,
                                            itemType: row.itemType
                                        },
                                        success: function (data) {
                                            if (data.success) {
                                                msgTool.success("恭喜您，领取状态已重置为已领取！", "信息提示");
                                                tableRefresh();
                                                dialogItself.close();
                                                return false;
                                            } else {
                                                msgTool.error("警告，" + data.msg, "错误提示");
                                                tableRefresh();
                                                dialogItself.close();
                                                return false;
                                            }
                                        },
                                        error: function (jqXHR) {
                                            msgTool.error(jqXHR.status + " (" + jqXHR.statusText + ")", "错误提示");
                                            tableRefresh();
                                            dialogItself.close();
                                            return false;
                                        }
                                    })
                                }
                            }]
                    });
                }

            };


            //table表格
            myTable.bootstrapTable({
                url: API_URL, // 接口 URL 地址
                cache: false, // 不缓存
                striped: true, // 隔行加亮
                pagination: true, // 开启分页功能
                sidePagination: 'server',//服务端分页
                sortOrder: 'desc', // 设置排序为反序 desc
                fixedColumns: true,//自动修复
                queryParams: queryParams,//搜索附加参数
                smartDisplay: false, // 智能显示 pagination 和 cardview 等
                toolbar: ".toolBar", //顶部工具菜单
                toolbarAlign: "right", //顶部工具位置
                mobileResponsive: "true", //手机模式自适应宽度（自动变成卡片模式）
                columns: [
                    {
                        field: 'itemName',
                        title: '公司信息',
                        width: '30%',
                        formatter: function (value) {
                            return '<i class="fa fa-hashtag text-primary" aria-hidden="true"></i>&emsp;<b>' + value + '</b>'
                        }
                    },
                    {
                        field: 'issueDatetime',
                        title: '签发信息',
                        width: '12%',
                        formatter: function (value, row) {
                            return '<i class="fa fa-user" aria-hidden="true"></i>&emsp;' + row.issueUserName + '<br /><i class="fa fa-clock-o" aria-hidden="true"></i>&emsp;' + value
                        }
                    }, {
                        field: 'issueStatus',
                        title: '签发状态',
                        align: 'center',
                        width: '10%',
                        formatter: function (value) {
                            if (value === 1) {
                                return "<span class=\"label label-primary\"><i class=\"fa fa-lock\" aria-hidden=\"true\"></i>&nbsp;已签发</span>";
                            } else {
                                return "<span class=\"label label-warning\"><i class=\"fa fa-lock\" aria-hidden=\"true\"></i>&nbsp;待签发</span>";
                            }
                        }
                    }, {
                        field: 'receiveDatetime',
                        title: '领取信息',
                        width: '12%',
                        formatter: function (value, row) {
                            return '<i class="fa fa-user" aria-hidden="true"></i>&emsp;' + row.receiveUserName + '<br /><i class="fa fa-clock-o" aria-hidden="true"></i>&emsp;' + value
                        }
                    }, {
                        field: 'receiveStatus',
                        title: '领取状态',
                        align: 'center',
                        width: '10%',
                        formatter: function (value) {
                            if (value === 1) {
                                return "<span class=\"label label-primary\"><i class=\"fa fa-lock\" aria-hidden=\"true\"></i>&nbsp;已领取</span>";
                            } else {
                                return "<span class=\"label label-warning\"><i class=\"fa fa-lock\" aria-hidden=\"true\"></i>&nbsp;待领取</span>";
                            }
                        }
                    },
                    {
                        field: 'action',
                        title: '操作',
                        align: 'center',
                        width: '39%',
                        formatter: operateFormatter,
                        events: operateEvents
                    }
                ]
            });

            //搜索刷新
            $('#search').on('click', function () {
                tableRefresh();
            });


            //搜索参数传递
            function queryParams(params) {
                params.search = $("#searchValue").val();
                params.merchantAddress = $("#merchantAddress").val();
                params.applyStatus = $("#applyStatus").val();
                params.applyTime = $("#applyTime").val();
                return {
                    limit: params.limit,
                    offset: params.offset,
                    applyTime: params.applyTime,
                    applyStatus: params.applyStatus,
                    merchantAddress: params.merchantAddress,
                    search: params.search
                };
            }

            //回显
            function responseHandler(res) {
                return res.rows;
            }


            //模态框
            function htmlModal(title, row) {
                BootstrapDialog.show({
                    type: BootstrapDialog.TYPE_DEFAULT,
                    size: BootstrapDialog.SIZE_WIDE,
                    tabindex: null,//解决select2搜索框失去焦点的问题
                    title: title,
                    closable: true,	//关闭箭头启动
                    closeByBackdrop: false,	//点击空白不消失
                    closeByKeyboard: true,//ESC取消禁用
                    message: $('<div></div>').load('../save/form.html?itemId=' + row.itemId + "&itemType=" + row.itemType),
                    buttons: [{
                        label: '取消',
                        cssClass: 'btn-white cancel',
                        icon: 'fa fa-times',
                        action: function (dialog) {
                            dialog.close();
                        }
                    }, {
                        label: '提交' + title,
                        //hotkey: 13,//回车提交
                        cssClass: 'btn-primary',
                        icon: 'fa fa-check',
                        action: function (dialog) {
                            //返回验证结果
                            var flag = $("#winModal").validate().form();
                            if (flag) {
                                $.baseUtils.ajax({
                                    dialog: dialog,
                                    url: '../save/action',
                                    data: $("#winModal").serialize(),
                                    text: "保存",
                                    isTable: true,
                                    tableName: '#dataList',
                                    isDialog: true
                                })
                            }
                        }
                    }]
                });
            }
        }
    };
});






